package com.jf.anmocker.plugin.mockcore.asm

import com.jf.mocker.anotations.IMethodMocker
import org.objectweb.asm.AnnotationVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Type
import org.objectweb.asm.commons.AdviceAdapter

class AnMockerMethodVisitor(api : Int, methodVisitor : MethodVisitor, access : Int, name : String?,val descriptor : String?) : AdviceAdapter(api, methodVisitor, access, name, descriptor) {

    private var methodMockerAno : AnMockerAnoVisitor? = null

    override fun visitAnnotation(descriptor: String?, visible: Boolean): AnnotationVisitor? {
        println("visitAnnotation descriptor:$descriptor visible:$visible");
        if(Type.getDescriptor(IMethodMocker::class.java).equals(descriptor)){
            val methodAno = AnMockerAnoVisitor(super.visitAnnotation(descriptor, visible))
            updateAnoInfo(methodAno)
            return methodAno
        }

        return super.visitAnnotation(descriptor, visible)
    }

    override fun onMethodEnter() {
        println("access:$access, name:$name , descriptor:$descriptor ===> onMethodEnter")
        super.onMethodEnter()
    }

    override fun onMethodExit(opcode: Int) {
        println("access:$access, name:$name , descriptor:$descriptor ===> onMethodExit opcode:$opcode   <===");
        super.onMethodExit(opcode)
    }

    private fun updateAnoInfo(annoInfo : AnMockerAnoVisitor){
        methodMockerAno = annoInfo
        println("updateAnoInfo[IMethodMocker] name = [${annoInfo.name}] value = [${annoInfo.value}]")
    }
}